//
//  EMNewGoodsViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/21.
//  Copyright (c) 2014年 cczu. All rights reserved.
//

#import "EMNewGoodsViewController.h"
#import "UINavigationController+EMNavi.h"
#import "EMNewContentCell.h"
#import "AppDelegate.h"
#import "EMEnterAndNewReqBody.h"
#import "EMEnterAndNewRespBody.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "EMGoodInfo3.h"
#import "EMDetailInfoViewController.h"
#import "UserInfo.h"
#import "EMLoginViewController.h"
@interface EMNewGoodsViewController ()
{
    AppDelegate *app ;
    NSString *titleImgUrl;
    NSString *titleGoodId;
    NSArray *goodArray;
}
@end

@implementation EMNewGoodsViewController
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationMode:EMNavigationModeNone andSubMode:0];
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [self getGoodInfo];
    [super viewWillAppear:animated];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"EMEnterHeaderCell"];
    [collectionView registerClass:[EMNewContentCell class] forCellWithReuseIdentifier:@"EMEnterCell"];
    collectionView.backgroundColor = RGB(155, 208, 52);
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return goodArray.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *cellIdentifier = @"EMEnterHeaderCell";
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        UIImageView *hIV = [[UIImageView alloc] init];
        [hIV sd_setImageWithURL:[NSURL URLWithString:titleImgUrl]];
        hIV.frame = CGRectMake(0, 0, W(300), H(100));
        [cell addSubview:hIV];
        
        
        return cell;

    } else {
        EMGoodInfo3 *goodInfo = [[EMGoodInfo3 alloc] initWithEnterGoodInfo:goodArray[indexPath.row ]];
        
        static NSString *cellIdentifier = @"EMEnterCell";
        EMNewContentCell *cell = (EMNewContentCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        [cell.goodImageView sd_setImageWithURL:[NSURL URLWithString:goodInfo.m_imgUrl]];
        cell.goodNameLabel.text = goodInfo.m_name;
        cell.goodDescLabel.text = (NSNull *)goodInfo.m_goodDesc == [NSNull null] ? @"" : goodInfo.m_goodDesc;
        cell.priceLabel.text = [NSString stringWithFormat:@"¥%@", goodInfo.m_nowPrice];
        if ([goodInfo.m_mode isEqualToString:@"0"]) {
            cell.originPriceLabel.text = @"";
        } else {
            NSMutableAttributedString *oPrice = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%@", goodInfo.m_originPrice]];
            [oPrice addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, goodInfo.m_originPrice.length+1)];
            cell.originPriceLabel.attributedText = oPrice;
        }
        cell.addToCart.tag = indexPath.row + 80000;
        [cell.addToCart addTarget:self action:@selector(addToCart:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    
}
- (void)addToCart:(UIButton *)btn
{
    if ([UserInfo sharedUserInfo].m_isLogin) {
        EMGoodInfo3 *info = [[EMGoodInfo3 alloc] initWithGoodInfo:goodArray[btn.tag - 80000]];
        [app addToCartWithID:info.m_id];
    } else {
        [app showTip:@"请先登录"];
        EMLoginViewController *controller = [[EMLoginViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:controller animated:YES];
    }
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    UIEdgeInsets top = {W(5), H(10), W(5), H(10)};
    return top;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(W(300), H(100));
    } else {
        return CGSizeMake(W(145), H(245));
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld %ld", indexPath.section ,indexPath.row);
    
    EMDetailInfoViewController *controller = [[EMDetailInfoViewController alloc] init];
    if (indexPath.section == 0) {
        controller.goodID = titleGoodId;
    } else {
        EMGoodInfo3 *goodInfo = [[EMGoodInfo3 alloc] initWithEnterGoodInfo:goodArray[indexPath.row ]];
        controller.goodID = goodInfo.m_id;
    }
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}
- (void)getGoodInfo
{
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    EMEnterAndNewReqBody *reqBody = [[EMEnterAndNewReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"MORE-GOODS-INFO";
    reqBody.FLAG = @"2";
    reqBody.GETTIMES = @"1";
    reqBody.PAGE = @"500";
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    // 模型转字典
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        // 将返回的字典转成模型，键的名称必须相同
        EMEnterAndNewRespBody *respBody = [EMEnterAndNewRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            
            titleImgUrl = respBody.PIC;
            titleGoodId = respBody.ID;
            goodArray = respBody.GOODSINFO;
            
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
    
}


@end
